package com.qbw.app.request.impl;

import android.text.TextUtils;

import com.http.okhttp.HttpTask;
import com.qbw.actionmanager.ActionManager;
import com.qbw.app.Constant;
import com.qbw.app.XApplication;
import com.qbw.app.action.UserInfoUpdateAction;
import com.qbw.app.manager.UserManager;
import com.qbw.app.model.BaseModel;
import com.qbw.app.model.User;
import com.qbw.app.model.UserInfo;
import com.qbw.app.request.IUserRequest;
import com.qbw.core.jpush.JPushUtil;
import com.qbw.spm.P;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;


/**
 * @author QBW
 * @date 2019/8/29
 */
public class UserRequest implements IUserRequest {
    /**
     * 登录
     */
    public static final String METHOD_LOGIN = "auth";
    /**
     * 获取用户信息
     */
    public static final String METHOD_USERINFO = "user/info";
    /**
     * 注册
     */
    public static final String METHOD_REGIST = "user/register";
    /**
     * 重置密码
     */
    public static final String METHOD_RESET_PASSWORD = "user/resetPwd";
    /**
     * 更新用户信息
     */
    public static final String METHOD_UPDATE_INFO = "user/update";

    @Override
    public HttpTask login(String telCode,
                          String phone,
                          String password,
                          HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("telCode", telCode);
        params.put("phone", phone);
        params.put("password", password);
        return HttpTask.create(METHOD_LOGIN,
                               params,
                               User.Response.class,
                               null,
                               new HttpTask.FlowCallBack() {
                                   @Override
                                   public void onSuccess(HttpTask httpTask,
                                                         Object entity,
                                                         String modelStr) {
                                       P.putString(Constant.KEY_LAST_LOGIN_COUNTRY_AREA, telCode);
                                       P.putString(Constant.KEY_LAST_LOGIN_PHONE, phone);
                                       User.Response response = (User.Response) entity;
                                       UserManager.getInstance().saveUser(response.getData());
                                   }

                                   @Override
                                   public void onFailed(HttpTask httpTask,
                                                        int errorCode,
                                                        String message) {

                                   }
                               },
                               callBack,
                               null).post();
    }

    @Override
    public HttpTask getUserInfo(HttpTask.CallBack callBack) {
        return HttpTask.create(METHOD_USERINFO, null, UserInfo.Response.class, callBack).get();
    }

    @Override
    public HttpTask regist(String telCode,
                           String phone,
                           String password,
                           String code,
                           HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("telCode", telCode);
        params.put("phone", phone);
        params.put("password", password);
        params.put("code", code);

        return HttpTask.create(METHOD_REGIST,
                               params,
                               User.Response.class,
                               null,
                               new HttpTask.SimpleFlowCallBack() {
                                   @Override
                                   public void onSuccess(HttpTask httpTask,
                                                         Object entity,
                                                         String modelStr) {
                                       P.putString(Constant.KEY_LAST_LOGIN_COUNTRY_AREA, telCode);
                                       P.putString(Constant.KEY_LAST_LOGIN_PHONE, phone);
                                       User.Response response = (User.Response) entity;
                                       UserManager.getInstance().saveUser(response.getData());
                                   }
                               },
                               callBack,
                               null).post();
    }

    @Override
    public HttpTask resetPassword(String telCode,
                                  String phone,
                                  String password,
                                  String code,
                                  HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("telCode", telCode);
        params.put("phone", phone);
        params.put("password", password);
        params.put("code", code);
        return HttpTask.create(METHOD_RESET_PASSWORD, params, BaseModel.class, callBack).post();
    }

    @Override
    public HttpTask updateInfo(String deviceToken,
                               String currentVehicleCode,
                               String userNick,
                               String userImgPath,
                               HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        if (TextUtils.isEmpty(deviceToken)) {
            deviceToken = JPushInterface.getRegistrationID(XApplication.getInstance());
        }
        if (!TextUtils.isEmpty(deviceToken)) {
            params.put("deviceToken", deviceToken);
        }
        if (!TextUtils.isEmpty(currentVehicleCode)) {
            params.put("currentVehicleCode", currentVehicleCode);
        }
        if (!TextUtils.isEmpty(userNick)) {
            params.put("userNick", userNick);
        }
        if (!TextUtils.isEmpty(userImgPath)) {
            params.put("userImgPath", userImgPath);
        }
//        return HttpTask.create(METHOD_UPDATE_INFO, params, BaseModel.class, callBack).post();

        return HttpTask.create(METHOD_UPDATE_INFO,
                               params,
                               BaseModel.class,
                               null,
                               new HttpTask.SimpleFlowCallBack() {
                                   @Override
                                   public void onSuccess(HttpTask httpTask,
                                                         Object entity,
                                                         String modelStr) {
                                       super.onSuccess(httpTask, entity, modelStr);
                                       ActionManager.getInstance()
                                                    .triggerAction(new UserInfoUpdateAction());
                                   }
                               }
                               ,callBack
                               , null
                               ).post();
    }
}
